//
//  YSExperienceOrderListCtrl.m
//  Yueshi
//
//  Created by Northwan on 15/8/3.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//  体验券页

#import "YSExperienceOrderListCtrl.h"
#import "JXScrollBar.h"
#import "YSCheckLabel.h"
#import "YSChildOrderListCtrl.h"

@interface YSExperienceOrderListCtrl() <UIScrollViewDelegate,JXScrollBarDelegate>

@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)NSMutableArray *childController;
@property(nonatomic,strong)NSMutableArray *titleArray;
@property(nonatomic,strong)NSMutableArray *labelArray;

@property(nonatomic,strong)JXScrollBar *scrollBar;

@end

@implementation YSExperienceOrderListCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    //    self.fd_prefersNavigationBarHidden = YES;
    
    _childController = [NSMutableArray array];
    _titleArray = [NSMutableArray array];
    _labelArray = [NSMutableArray array];
    
    [self initLabel];
    
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    
    scrollView.frame = CGRectMake(0, 0, SCREENW, SCREENH);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.pagingEnabled = YES;
    //设置scrollview的代理
    scrollView.delegate = self;
    scrollView.scrollsToTop = NO;
     scrollView.delaysContentTouches = NO;//这句代码是解决scrollview和tableview的滚动冲突
    _scrollView = scrollView;
    [self.view addSubview:scrollView];
    
    JXScrollBar *scrollBar = [[JXScrollBar alloc]initWithFrame:CGRectMake(0, 0, SCREENW-16, 44) type:JXScrollBarSplitTypeDefault];
    scrollBar.delegate = self;
    _scrollBar = scrollBar;
    [self.view addSubview:_scrollBar];
    [self inflateLayout];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(updateOrderStatus:) name:kSubviewsUpdateOrderStatus object:nil];
    
}

-(void)updateOrderStatus:(NSNotification *)nortif{
    for (YSChildOrderListCtrl *child in _childController) {
        [child requestData];
    }
}

-(void)initLabel{
    YSCheckLabel *label1 = [YSCheckLabel new];
    [label1 setId:0];
    [label1 setName:@"全部"];
    [_labelArray addObject:label1];
    
    YSCheckLabel *label2 = [YSCheckLabel new];
    [label2 setId:10];
    [label2 setName:@"待支付"];
    [_labelArray addObject:label2];
    
    YSCheckLabel *label3 = [YSCheckLabel new];
    [label3 setId:11];
    [label3 setName:@"待消费"];
    [_labelArray addObject:label3];
    
    /*YSCheckLabel *label4 = [YSCheckLabel new];
    [label4 setId:14];
    [label4 setName:@"退款单"];
    [_labelArray addObject:label4];*/

}

-(void)inflateLayout{
    for (UIViewController *ctrl in _childController) {
        [ctrl.view removeFromSuperview];
    }
    [_childController removeAllObjects];
    //    [_titleArray removeAllObjects];
    
    //添加子页
    for (int i=0; i<_labelArray.count; i++){
        YSCheckLabel *label =  _labelArray[i];
        [_titleArray addObject:label.name];
        YSChildOrderListCtrl *controller = [YSChildOrderListCtrl initWithCheckLabel:label];
        [controller.view setTag:2];
        [_childController addObject:controller];
        
        UIView *view = controller.view;
        view.frame =CGRectMake(i*SCREENW, 0, SCREENW, self.view.height-NavWithStatusBarH);
        [_scrollView addSubview:view];
    }
    
    //设置scrollBar的数据
    [_scrollBar setTitleArray:_titleArray];
    [_scrollBar scrollViewDidScroll:_scrollView];
    
    //使第1页去加载数据
    [self scrollViewDidEndDecelerating:_scrollView];
    _scrollView.contentSize = CGSizeMake(_titleArray.count*SCREENW, 0);
}

#pragma mark -jxscrollbar代理，处理点击title的button事件
- (void)titleClicked:(JXScrollBar *)scrollbar index:(int)index{
    [scrollbar autoScrollToCenter:index];
    [self setScrollCurrentPageNum:index];
}

#pragma mark -滚动代理
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [_scrollBar scrollViewDidScroll:scrollView];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //当前页数
    int pageNum = (scrollView.contentOffset.x+scrollView.width*0.5)/scrollView.width;
    
    [self setScrollCurrentPageNum:pageNum];
    [_scrollBar scrollViewDidEndDecelerating:scrollView];
}

-(void)setScrollCurrentPageNum:(int)pageNum{
    CGPoint p = CGPointMake(pageNum*_scrollView.frame.size.width, 0);
    [_scrollView setContentOffset:p];
    //这句代码的作用是解决点击顶部之后底部的scrollview往上跑的问题
    _scrollView.y = 0;
    if ([_childController[pageNum] isKindOfClass:[YSChildOrderListCtrl class]]) {
        YSChildOrderListCtrl *controller = _childController[pageNum];
        //调用请求数据的方法
        [controller checkRequestData];
    }
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

@end
